package com.whisper.application.system.web.controller.system;

import com.whisper.common.annotation.Log;
import com.whisper.common.config.WhisperConfig;
import com.whisper.common.core.controller.BaseController;
import com.whisper.common.core.domain.AjaxResult;
import com.whisper.common.enums.BusinessType;
import com.whisper.common.exception.operate.OperateException;
import com.whisper.common.utils.StreamUtil;
import com.whisper.common.utils.file.FileUploadUtils;
import com.whisper.common.utils.file.MimeTypeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 个人信息 业务处理
 * 
 * @author whisper
 */
@RestController
@RequestMapping("/system/art/profile")
public class SysArtProfileController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(SysArtProfileController.class);

    /**
     * 上传文章图片
     */
    @Log(title = "上传文章图片", businessType = BusinessType.UPLOAD)
    @PostMapping("/image")
    public void uploadPostImage(@RequestParam("image") MultipartFile file, Long id, HttpServletResponse response) throws Exception
    {
        try {
            if (!file.isEmpty()) {
                String image = FileUploadUtils.upload(WhisperConfig.getImagePath(), file, MimeTypeUtils.IMAGE_EXTENSION);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", image);
                StreamUtil.response(response, ajax);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OperateException("上传图片异常，请联系管理员");
        }
    }
}
